﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace dotNetInteropPlugin.CSharpMixingLibrary
{
    internal class PluginInfoCollection: ICollection<PluginInformation>
    {
        public PluginInfoCollection()
        {
            inPluginsList = new List<PluginInformation>();
        }
        public PluginInfoCollection(IEnumerable<PluginInformation> col)
        {
            inPluginsList = new List<PluginInformation>(col);
            foreach (PluginInformation item in col)
            {
                item.PluginLoadEvent += new PluginLoadUnloadEvent(item_PluginLoadEvent);
                item.PluginUnloadEvent += new PluginLoadUnloadEvent(item_PluginUnloadEvent);
            }
        }


        private List<PluginInformation> inPluginsList = null;


        public PluginInformation this[int id]
        {
            get { return inPluginsList[id]; }
        }


        public void Add(PluginInformation item)
        {
            inPluginsList.Add(item);
            item.PluginLoadEvent += new PluginLoadUnloadEvent(item_PluginLoadEvent);
            item.PluginUnloadEvent += new PluginLoadUnloadEvent(item_PluginUnloadEvent);
        }

        public void AddRange(IEnumerable<PluginInformation> col)
        {
            inPluginsList.AddRange(col);
            foreach (PluginInformation item in col)
            {
                item.PluginLoadEvent += new PluginLoadUnloadEvent(item_PluginLoadEvent);
                item.PluginUnloadEvent += new PluginLoadUnloadEvent(item_PluginUnloadEvent);
            }
        }

        public void Clear()
        {
            UnloadAll();
            foreach (PluginInformation item in inPluginsList)
            {
                item.PluginLoadEvent -= new PluginLoadUnloadEvent(item_PluginLoadEvent);
                item.PluginUnloadEvent -= new PluginLoadUnloadEvent(item_PluginUnloadEvent);
            }
            inPluginsList.Clear();
        }

        public bool Contains(PluginInformation item)
        {
            return inPluginsList.Contains(item);
        }

        public void CopyTo(PluginInformation[] array, int arrayIndex)
        {
            inPluginsList.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return inPluginsList.Count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool Remove(PluginInformation item)
        {
            bool Res = inPluginsList.Remove(item);
            if (Res)
            {
                item.Unload();
                item.PluginLoadEvent -= new PluginLoadUnloadEvent(item_PluginLoadEvent);
                item.PluginUnloadEvent -= new PluginLoadUnloadEvent(item_PluginUnloadEvent);
            }
            return Res;
        }

        public IEnumerator<PluginInformation> GetEnumerator()
        {
            return inPluginsList.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }



        public void UnloadAll()
        {
            foreach (PluginInformation inf in inPluginsList)
                inf.Unload();
        }


        public event PluginLoadUnloadEvent PluginLoadEvent;
        public event PluginLoadUnloadEvent PluginUnloadEvent;

        void item_PluginLoadEvent(PluginInformation sender)
        {
            if (PluginLoadEvent != null)
                PluginLoadEvent(sender);
        }
        void item_PluginUnloadEvent(PluginInformation sender)
        {
            if (PluginUnloadEvent != null)
                PluginUnloadEvent(sender);
        }
    }
}
